﻿// Decompiled with JetBrains decompiler
// Type: Bannerlord.ModuleManager.AlphanumComparatorFast
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

#nullable enable
namespace Bannerlord.ModuleManager
{
  internal sealed class AlphanumComparatorFast : IComparer<string?>, IComparer
  {
    public int Compare(object? x, object? y) => this.Compare(x as string, y as string);

    public int Compare(string? s1, string? s2)
    {
      if (s1 == null && s2 == null)
        return 0;
      if (s1 == null)
        return -1;
      if (s2 == null)
        return 1;
      int length1 = s1.Length;
      int length2 = s2.Length;
      if (length1 == 0 && length2 == 0)
        return 0;
      if (length1 == 0)
        return -1;
      if (length2 == 0)
        return 1;
      int index1 = 0;
      int index2 = 0;
      while (index1 < length1 || index2 < length2)
      {
        if (index1 >= length1)
          return -1;
        if (index2 >= length2)
          return 1;
        char ch1 = s1[index1];
        char ch2 = s2[index2];
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        while (index1 < length1 && (stringBuilder1.Length == 0 || AlphanumComparatorFast.InChunk(ch1, stringBuilder1[0])))
        {
          stringBuilder1.Append(ch1);
          ++index1;
          if (index1 < length1)
            ch1 = s1[index1];
        }
        while (index2 < length2 && (stringBuilder2.Length == 0 || AlphanumComparatorFast.InChunk(ch2, stringBuilder2[0])))
        {
          stringBuilder2.Append(ch2);
          ++index2;
          if (index2 < length2)
            ch2 = s2[index2];
        }
        if (char.IsDigit(stringBuilder1[0]) && char.IsDigit(stringBuilder2[0]))
        {
          int int32_1 = Convert.ToInt32(stringBuilder1.ToString());
          int int32_2 = Convert.ToInt32(stringBuilder2.ToString());
          if (int32_1 < int32_2)
            return -1;
          if (int32_1 > int32_2)
            return 1;
        }
        else
        {
          int num = string.CompareOrdinal(stringBuilder1.ToString(), stringBuilder2.ToString());
          if (num >= 1)
            return 1;
          if (num <= -1)
            return -1;
        }
      }
      return 0;
    }

    private static bool InChunk(char ch, char otherCh)
    {
      AlphanumComparatorFast.ChunkType chunkType = AlphanumComparatorFast.ChunkType.Alphanumeric;
      if (char.IsDigit(otherCh))
        chunkType = AlphanumComparatorFast.ChunkType.Numeric;
      return (chunkType != AlphanumComparatorFast.ChunkType.Alphanumeric || !char.IsDigit(ch)) && (chunkType != AlphanumComparatorFast.ChunkType.Numeric || char.IsDigit(ch));
    }

    private enum ChunkType
    {
      Alphanumeric,
      Numeric,
    }
  }
}
